#include "gui.h"

#define MENU_DEFAULT_X  30
#define MENU_DEFAULT_Y  120

void gui_menu_draw(GUI_Menu *menu)
{
    GFX_Sprite *cursor = gfx_sprite_new("gfx_font");
    cursor->frame = '\004';

    // Draw each item
    for(int i = 0; i < menu->nb_items; i++)
    {
        if(menu->curItem==i)
        {
            cursor->x = menu->items[i].caption->x-15;
            cursor->y = menu->items[i].caption->y;
            gfx_render_sprite(cursor);
        }
        gfx_render_string(menu->items[i].caption);
    }
    gfx_sprite_delete(cursor);
}

GUI_Menu *gui_menu_create(const char *title, const char **captions)
{
    GUI_Menu *menu=(GUI_Menu*)mem_allocate(sizeof(GUI_Menu));
    // Set up default position
    menu->x = MENU_DEFAULT_X;
    menu->y = MENU_DEFAULT_Y;
    menu->items = array_new(1, sizeof(GUI_MenuItem));
    menu->curItem = 0;

    // Fill in the items' caption
    int i;
    for(i = 0; captions[i] != 0; i++)
    {
        menu->items = array_resize(menu->items, array_size(menu->items)+1);
        menu->items[i].caption=gfx_string_create("gfx_font", captions[i]);
        menu->items[i].caption->y = (i+7.5) * (VSCREEN_HEIGHT/15);
        menu->items[i].caption->x = VSCREEN_WIDTH/3.1;

    }
    menu->nb_items = i;
    return menu;
}

void gui_menu_delete(GUI_Menu *menu)
{
    for(int i = 0; i < array_size(menu->items); i++)
    {
        gfx_string_delete(menu->items[i].caption);
    }
    array_delete(menu->items);
    mem_free(menu);
    return;
}

MenuKey gui_menu_key(GUI_Menu *menu)
{
    int buttons = io_player_get_buttons_menu();

    // Menu movement
    if(buttons & PLAYER_UP)
    {
        sfx_play(1, SFX_SOUND_MENUDEPLA);
        return MENUKEY_UP;
    }
    else if(buttons & PLAYER_DOWN)
    {
        sfx_play(1, SFX_SOUND_MENUDEPLA);
        return MENUKEY_DOWN;
    }

    // Menu validation
    else if(buttons & PLAYER_B)
    {
        sfx_play(1, SFX_MENU_ENTER);
        return MENUKEY_ENTER;
    }

    // Nothing
    else
    {
        return MENUKEY_NONE;
    }
}


//
//-----------------------------------------------------------------------------
//


void gui_menu_main(void)
{
    const char *captions[] = {"New Game", "Editor", "Options", "Quit", 0} ;
    int end = 0;

    GUI_Menu *menu = gui_menu_create("main menu", captions);
    GFX_Sprite *spr=gfx_sprite_new("gfx_font2");
    GFX_Sprite *spr1=gfx_sprite_new("gfx_laby");
    spr1->x=menu->x+300;
    spr1->y=menu->y+125;

    do
    {
        gfx_scene_begin();
        gfx_render_sprite(spr);
        gfx_render_sprite(spr1);
        gui_menu_draw(menu);
        gfx_scene_end();

        MenuKey key = gui_menu_key(menu);
        if (key==MENUKEY_UP)
        {
            menu->curItem--;
            sfx_play(0, SFX_SOUND_MENUDEPLA);
            if(menu->curItem < 0) menu->curItem = 0;
        }
        else if(key==MENUKEY_DOWN)
        {
            menu->curItem++;
            sfx_play(0, SFX_SOUND_MENUDEPLA);
            if(menu->curItem >= menu->nb_items) menu->curItem = menu->nb_items - 1;
        }
        else if (key==MENUKEY_ENTER)
        {
            switch(menu->curItem)
            {
            case 0 :
                start_local_game();
                break;
            case 1 :

                editor_level_main();
                break;
            case 2 :

                gui_menu_options();
                break;
            case 3 :

                end=1;
                break;
            }
        }
        time_sleep(1./30);
    }
    while(end==0);

    gfx_sprite_delete(spr);
    gfx_sprite_delete(spr1);
    gui_menu_delete(menu);
    return;
}

void gui_menu_multiplayer(void)
{
    const char *captions[] = {"Local 2P", "Join", "Host", "Return", 0} ;
    int end = 0;

    GUI_Menu *menu = gui_menu_create("main menu", captions);
    GFX_Sprite *spr=gfx_sprite_new("gfx_font2");
    GFX_Sprite *spr1=gfx_sprite_new("gfx_laby");
    spr1->x=menu->x+300;
    spr1->y=menu->y+125;

    do
    {
        gfx_scene_begin();
        gfx_render_sprite(spr);
        gfx_render_sprite(spr1);
        gui_menu_draw(menu);
        gfx_scene_end();

        MenuKey key = gui_menu_key(menu);
        if (key==MENUKEY_UP)
        {
            menu->curItem--;
            sfx_play(0, SFX_SOUND_MENUDEPLA);
            if(menu->curItem < 0) menu->curItem = 0;
        }
        else if(key==MENUKEY_DOWN)
        {
            menu->curItem++;
            sfx_play(0, SFX_SOUND_MENUDEPLA);
            if(menu->curItem >= menu->nb_items) menu->curItem = menu->nb_items - 1;
        }
        else if (key==MENUKEY_ENTER)
        {
            switch(menu->curItem)
            {
            case 0 :
                sfx_play(1, SFX_MENU_ENTER);
                start_local_game();
                break;
            case 1 :
                sfx_play(1, SFX_MENU_ENTER);
                break;
            case 2 :
                sfx_play(1, SFX_MENU_ENTER);
                break;
            case 3 :
                sfx_play(1, SFX_MENU_ENTER);

                end=1;
                break;
            }
        }
        time_sleep(1./30);
    }
    while(end==0);

    gfx_sprite_delete(spr);
    gfx_sprite_delete(spr1);
    gui_menu_delete(menu);
    return;
}

void gui_menu_options(void)
{
    const char *optn_captions[] = {"Sound", "Resolution", "Return", 0} ;
    int end=0;
    GUI_Menu *optn = gui_menu_create("option", optn_captions);

    GFX_Sprite *spr=gfx_sprite_new("gfx_font2");
    GFX_Sprite *spr1=gfx_sprite_new("gfx_roueoptn");
    spr1->x=optn->x+300;
    spr1->y=optn->y+125;

    do
    {
        gfx_scene_begin();
        gfx_render_sprite(spr);
        gfx_render_sprite(spr1);
        gui_menu_draw(optn);
        gfx_scene_end();

        MenuKey key = gui_menu_key(optn);
        if (key==MENUKEY_UP)
        {
            sfx_play(0, SFX_SOUND_MENUDEPLA);
            optn->curItem--; if(optn->curItem<0) optn->curItem=0;
        }
        else if(key==MENUKEY_DOWN)
        {
            sfx_play(0, SFX_SOUND_MENUDEPLA);
            optn->curItem++; if(optn->curItem >= optn->nb_items) optn->curItem = optn->nb_items - 1;
        }
        else if (key==MENUKEY_ENTER)
        {
            switch(optn->curItem)
            {
            case 0 :

                gui_menu_sound();
                break;
            case 1 :

                gui_menu_graphics();
                break;
            case 2 :

                end=1;
                break;
            }
        }
        time_sleep(1./30);
    }
    while(end==0);

    gui_menu_delete(optn);
    gfx_sprite_delete(spr);
    gfx_sprite_delete(spr1);
    return;
}

void gui_menu_graphics(void)
{
    const char *Resolution_captions[]= {"640/480", "1280/960", "Full Screen", "Return", 0};
    int end=0;
    GUI_Menu *Resolution= gui_menu_create("Resolution", Resolution_captions);
    GFX_Sprite *spr=gfx_sprite_new("gfx_font2");
    int n=1;
    GFX_Resolution res=gfx_get_resolution();
    do
    {
        gfx_scene_begin();
        gfx_render_sprite(spr);
        gui_menu_draw(Resolution);
        gfx_scene_end();

        MenuKey key = gui_menu_key(Resolution);
        if (key==MENUKEY_UP)
        {
            sfx_play(0, SFX_SOUND_MENUDEPLA);
            Resolution->curItem--;
            if(Resolution->curItem<0) Resolution->curItem=0;
        }
        else if(key==MENUKEY_DOWN)
        {
            sfx_play(0, SFX_SOUND_MENUDEPLA);
            Resolution->curItem++;
            if(Resolution->curItem >= Resolution->nb_items)
                Resolution->curItem = Resolution->nb_items - 1;
        }
        else if (key==MENUKEY_ENTER)
        {

            switch(Resolution->curItem)
            {
            case 0 :
                n=1;
                break;
            case 1 :
                n=2;
                break;
            case 2 :
                res.fullscreen=!res.fullscreen;
                    break;
            case 3 :
                end=1;
                break;
            }
            if(Resolution->curItem != 3)
            {
                res.width = 640*n;
                res.height = 480*n;
                gfx_set_resolution(res);
            }
        }
        time_sleep(1./30);
    }while(end==0);
     gui_menu_delete(Resolution);
     gfx_sprite_delete(spr);
    return;
}

void gui_menu_sound(void)
{
    const char *sound_captions[] = {"ON", "OFF", "Return", 0} ;
    int end=0;

    GUI_Menu *sound = gui_menu_create("sound", sound_captions);
    GFX_Sprite *spr=gfx_sprite_new("gfx_font2");
    GFX_Sprite *spr1=gfx_sprite_new(0);
    spr1->x=sound->x+310;
    spr1->y=sound->y+125;

    do
    {
        if(sfx_get_status() == 1)
        {
            spr1->image = gfx_image_get("gfx_soundon");
        }
        else
        {
            spr1->image = gfx_image_get("gfx_soundoff");
        }

        gfx_scene_begin();
        gfx_render_sprite(spr);
        gfx_render_sprite(spr1);
        gui_menu_draw(sound);
        gfx_scene_end();

        MenuKey key = gui_menu_key(sound);
        if (key==MENUKEY_UP)
        {
            sfx_play(0, SFX_SOUND_MENUDEPLA);
            sound->curItem--;
            if(sound->curItem<0) sound->curItem=0;
        }
        else if(key==MENUKEY_DOWN)
        {
            sfx_play(0, SFX_SOUND_MENUDEPLA);
            sound->curItem++;
            if(sound->curItem >= sound->nb_items) sound->curItem = sound->nb_items - 1;
        }
        else if (key==MENUKEY_ENTER)
        {
            switch(sound->curItem)
            {
            case 0 :
                sfx_set_status(1);

                break;
            case 1 :
                sfx_set_status(0);

                break;
            case 2 :

                end=1;
                break;
            }
        }
        time_sleep(1./30);
    }
    while(end==0);

    gfx_sprite_delete(spr);
    gfx_sprite_delete(spr1);
    gui_menu_delete(sound);
    return;
}

// 0 : Rien
// 1 : Resume
// 2 : Quit
int gui_menu_pause(int *curItem, int h)
{
    const char *captions[] = {"Resume", "Quit", 0} ;
    int end = 0;

    GUI_Menu *menu = gui_menu_create("Pause", captions);
    menu->curItem = *curItem;

    for(int i = 0; captions[i] != 0; i++)
    {
        menu->items[i].caption->y = i*15 + h/2;
    }

    do
    {
        gui_menu_draw(menu);

        MenuKey key = gui_menu_key(menu);
        if (key==MENUKEY_UP)
        {
            sfx_play(0, SFX_SOUND_MENUDEPLA);
            menu->curItem--;
            if(menu->curItem < 0) menu->curItem = 0;
        }
        else if(key==MENUKEY_DOWN)
        {
            sfx_play(0, SFX_SOUND_MENUDEPLA);
            menu->curItem++;
            if(menu->curItem >= menu->nb_items) menu->curItem = menu->nb_items - 1;
        }
        else if (key==MENUKEY_ENTER)
        {
            switch(menu->curItem)
            {
            case 0 :
                gui_menu_delete(menu);
                return 1;
            case 1 :
                gui_menu_delete(menu);
                return 2;
            }
        }
    } while(0);
    *curItem = menu->curItem;

    gui_menu_delete(menu);
    return 0;
}
